package com.park.web.controller.admin;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.resource.ResourceFileModel;
import com.park.client.model.user.IntegralIncomeModel;
import com.park.client.model.user.UserModel;
import com.park.client.service.resource.ResouceFileService;
import com.park.client.service.user.IntegralIncomeService;
import com.park.client.service.user.UserService;
import com.park.common.util.Page;
import com.park.common.util.PageData;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;

/**
 * 资源上传
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin/resource")
public class AdminResourceController extends BaseController{
	
	@Autowired
	private ResouceFileService resouceFileService;
	@Autowired
	private IntegralIncomeService incomeService;
	@Autowired
	private UserService userService;
	
	@RequestMapping("resourceList")
	public String resourceList(HttpServletRequest request){
			return Global.ADMIN_PATH+"resource/resourceList";
	}
	
	@RequestMapping("listRows")
	@ResponseBody
	public PageData<ResourceFileModel> listRows(Integer rows,@RequestParam(name="page",defaultValue="1")Integer pageNow,HttpServletRequest request){
		PageData<ResourceFileModel> pageData = new PageData<>();
		ResourceFileModel resourceFile = new ResourceFileModel();
		Integer total = resouceFileService.selectTotal(resourceFile);
		Page page = new Page(pageNow,total,rows);
		resourceFile.setStartPos(page.getStartPos());
		resourceFile.setPageSize(page.getPageSize());
		List<ResourceFileModel> list = resouceFileService.selectPagination(resourceFile);
		pageData.setTotal(total);
		pageData.setRows(list);
		return pageData;
	}
	
	/**
	 * 删除文件
	 */
	@RequestMapping("deleteById")
	@ResponseBody
	public Result<String> deleteById(Long id,HttpServletRequest request){
		Result<String> result = new Result<>();
		boolean success = resouceFileService.deleteByPrimaryKey(id);
		if(success){
			result.setMessage("删除成功!");
		}else{
			result.setSuccess(false);
			result.setMessage("删除失败!");
		}
			return result;
	}
	
	/**
	 * 添加文件
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result<String> save(ResourceFileModel resourceFile,HttpServletRequest request){
		Result<String> json = new Result<>();
			boolean success = resouceFileService.insertSelective(resourceFile);
			if(success){
				json.setSuccess(true);
				json.setMessage("添加成功!");
			}else{
				json.setSuccess(false);
				json.setMessage("添加失败!");
			}
			return json;
	}
	
	/**
	 * 修改文件
	 */
	@RequestMapping("edit")
	@ResponseBody
	public Result<String> edit(ResourceFileModel resourceFile,HttpServletRequest request){
		Result<String> json = new Result<>();
		boolean success = resouceFileService.updateByPrimaryKeySelective(resourceFile);
		if(success){
			json.setSuccess(true);
			json.setMessage("修改成功!");
		}else{
			json.setSuccess(false);
			json.setMessage("修改失败!");
		}
		return json;
	}
	
	/**
	 * 修改文件状态
	 */
	@RequestMapping("editFilePass")
	@ResponseBody
	public Result<String> editFilePass(@RequestParam("ids")String ids,HttpServletRequest request){
		Result<String> json = new Result<>();
		try {
			for (String id : ids.split(",")) {
				ResourceFileModel resourceFile = resouceFileService.selectByPrimaryKey(Long.parseLong(id));
				resourceFile.setIsPass(1);
				resouceFileService.updateByPrimaryKeySelective(resourceFile);
				//积分收入增加记录
				IntegralIncomeModel income = new IntegralIncomeModel();
				income.setCreateDate(new Date());
				income.setIntegral(resourceFile.getScore());
				income.setUserId(resourceFile.getId());
				income.setSource(0);
				incomeService.insertSelective(income);
				//user表增加积分
				UserModel u = userService.selectByPrimaryKey(resourceFile.getCreater());
				u.setIntegral(u.getIntegral()+resourceFile.getScore());
				userService.updateByPrimaryKeySelective(u);
			}
			json.setSuccess(true);
			json.setMessage("修改成功!");
		} catch (Exception e) {
			e.printStackTrace();
			json.setSuccess(false);
			json.setMessage("修改失败!");
		}
		return json;
	}
	
	
}
